package com.asm.DBloader;




import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;



public class ASM_FasmDS 
{
	
	
	private SQLiteDatabase dB;
	private ASM_DB dbHelper;
	Context context;

	
	public ASM_FasmDS (Context context)
	{
		this.context = context;
		dbHelper = new ASM_DB(context);
	}
	
	public void open() throws SQLException
	{
		dB = dbHelper.getWritableDatabase();
	}
	
	
	public int Create(ASM_Fasm det) {
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		
		 
//		Cursor cursor = dB.rawQuery("select * from fASM ", null);
//		
//		if(cursor.getCount()>0)
//			return -1;	
		
		ContentValues values = new ContentValues();
		values.put(ASM_DB.ASM_ASMCODE, det.getASM_ASMCODE());
		values.put(ASM_DB.ASM_ASMNAME, det.getASM_ASMNAME());
		values.put(ASM_DB.ASM_RSMCODE, det.getASM_RSMCODE());
		values.put(ASM_DB.ASM_ASMMOB, det.getASM_ASMMOB());
		values.put(ASM_DB.ASM_ASMTEL, det.getASM_ASMTEL());
		values.put(ASM_DB.ASM_ASMMAIL, det.getASM_ASMMAIL());
		values.put(ASM_DB.ASM_STATUS, det.getASM_STATUS());
		values.put(ASM_DB.ASM_ADDUSER, det.getASM_ADDUSER());
		values.put(ASM_DB.ASM_ADDDATE, det.getASM_ADDDATE());
		values.put(ASM_DB.ASM_ADDMACH, det.getASM_ADDMACH());
		values.put(ASM_DB.ASM_RECORDID, det.getASM_RECORDID());
		values.put(ASM_DB.ASM_ASMPREFIX, det.getASM_ASMPREFIX());
		count = (int) dB.insert(ASM_DB.ASM_TABLE, null, values);

		dB.close();
		return count;
		
	}
	
	public ArrayList<String> getFasm() 
	{
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<String> Details = new ArrayList<String>();
	
		String selectQuery = "select ASMCode from fASM";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
			int lkk = cursor.getCount();
			//for(int i = 0;i<cursor.getCount();i++)
			//{
			Log.v("ASMDB LISTDS", Integer.toString(lkk));
			Details.add(cursor.getString(0));
			//}
			
		}
		
		return Details;
	}
	
	public String getASMname(String ASMCode) 
	{
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
	
		String ASMName = null;
		String selectQuery = "select ASMName from fASM where ASMCode = '"+ASMCode+"'";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
		
			ASMName = cursor.getString(0);
		
		}
		
		return ASMName;
	}
}
